﻿// This source file is part of WallpaperControl, a tool for automatic changing 
// of the desktop wallpaper.
// 
// Visit http://wpcontrol.sourceforge.net/ for more.
//
// Copyright (c) 2014 nscrolldev
//
// Licensed under the GPLv2.
// See http://www.gnu.org/licenses/gpl-2.0.html for details.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WallpaperControl
{
    /// <summary>
    /// Condition that changes the wallpaper after a number of days.
    /// </summary>
    internal class CondDayInterval : Condition
    {
        /// <summary>
        /// Creates a new condition of type day interval.
        /// </summary>
        /// <param name="intervalDays">
        /// The number of days after which the wallpaper should be changed.
        /// </param>
        public CondDayInterval(int intervalDays)
        {
            this.IntervalDays = intervalDays;

            Program.ControlPanel.radioButtonDay.Checked = true;
        }

        internal const int CondId = 4;

        /// <summary>
        /// The Date when the wallpaper was changed.
        /// </summary>
        private DateTime _lastChange;

        /// <summary>
        /// The number of days after which the wallpaper should be changed.
        /// </summary>
        private int _intervalDays;

        /// <summary>
        /// Gets the ID of this condition.
        /// </summary>
        internal override int Id
        {
            get
            {
                return CondDayInterval.CondId;
            }
        }

        /// <summary>
        /// Gets or sets on what date the wallpaper was changed.
        /// </summary>
        /// <value>The System.DateTime when the last wallpaper change was 
        /// executed.</value>
        public DateTime LastChange
        {
            get
            {
                return this._lastChange;
            }
            set
            {
                this._lastChange = value;
            }
        }

        /// <summary>
        /// Gets or sets the interval after which the wallpaper should be changed.
        /// </summary>
        public int IntervalDays
        {
            get
            {
                return this._intervalDays;
            }
            set
            {
                if (value > 0)
                {
                    this._intervalDays = value;
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
        }

        /// <summary>
        /// The remaining time is unknown.
        /// </summary>
        internal override TimeSpan RemainingTime
        {
            get
            {
                return TimeSpan.Zero;
            }
        }

        internal override object Value
        {
            get
            {
                return this.IntervalDays;
            }
            set
            {
                this.IntervalDays = Convert.ToInt32(value);
            }
        }

        /// <summary>
        /// Possible values for the day interval.
        /// </summary>
        internal static int[] Days = new int[]
        {
            // Daily
            1,
            // 2 days
            2,
            // 3 days
            3,
            // 4 days
            4,
            // 5 days
            5,
            // 6 days
            6,
            // Weekly
            7,
            // 2 weeks
            14,
            // 3 weeks
            21,
            // Monthly
            30,
            // 2 months
            60,
            // 3 months
            90,
            // 4 months
            120,
            // 5 months
            150,
            // 6 months
            180
        };

        /// <summary>
        /// Checks if the current condition is true and executes the wallpaper 
        /// change.
        /// </summary>
        internal override void Check()
        {
            TimeSpan elapsedTime = DateTime.Now - this.LastChange;
            if (elapsedTime.Days == this.IntervalDays)
            {
                WallpaperManager.Instance.SetNextWallpaper();
                this.LastChange = DateTime.Now;
            }
        }

        /// <summary>
        /// Nothing to handle.
        /// </summary>
        internal override void ManualChangeCallback()
        {
        }

        internal override void StopSession()
        {
        }

        /// <summary>
        /// Reads the value from the combobox and applies it.
        /// </summary>
        internal override void ApplyValue()
        {
            this.IntervalDays = Days[Program.ControlPanel.comboBoxDay.SelectedIndex];
        }
    }
}
